package com.action.photo;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.beans.photo.PhotoReply;
import com.business.photo.PhotoReplyBus;
import com.business.user.UserInfoBus;
import com.config.SystemGlobals;
import com.util.MD5;
import com.util.TextTools;


public class ReplyPhoto extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String contentType = SystemGlobals.getValue("contentType");
		PrintWriter out = response.getWriter();
		response.setContentType(contentType);
		String photoNo = request.getParameter("id");
		String nickname = request.getParameter("nickname");
		String password = request.getParameter("password");
		String authenCode = request.getParameter("authencode");
		String replyContent = request.getParameter("replycontent");
		replyContent = TextTools.txt2html(replyContent);
		String passwordMD5 = new MD5().getMD5ofStr(password);
		UserInfoBus uib = new UserInfoBus();
		if (uib.checkLogin(nickname, passwordMD5)) {
			String AuthenCode = (String) request.getSession().getAttribute(
					"AuthenCode");
			if (!authenCode.equals(AuthenCode)) {
				out
						.print("<script>alert(\"验证码不对，请重新输入\");history.back();</script>");
			} else {
				PhotoReply pr = new PhotoReply();
				pr.setPhotoNo(photoNo);
				pr.setNickname(nickname);
				pr.setReplyContent(replyContent);
				SimpleDateFormat sdf = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");
				pr.setReplyTime(sdf.format(new Date()));
				PhotoReplyBus prb = new PhotoReplyBus();
				prb.add(pr);
				out.print("<script>alert(\"成功回复此图片\");window.location=\"photo/temp/"+photoNo+".html\";</script>");
			}

		}

		else {
		
				out
					.print("<script>alert(\"未登录成功\");window.location=\"user/login.jsp\";</script>");
			out.flush();
			out.close();
	}
	}

}
